<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>观察者模式</title>
</head>
<body>
<script>
  // 发布者-目标
  // Dependence
  class Dep {
    constructor() {
      this.subs = []
    }
    addSub(sub) {
      if (sub?.update) {
        this.subs.push(sub);
      }
    }
    notify() {
      this.subs.forEach(sub => sub.update())
    }
  }
  // 订阅者-观察者
  class Watcher {
    update() {
      console.log('update')
    }
  }
  const dep = new Dep();
  const watcher = new Watcher();
  dep.addSub(watcher);
  dep.addSub(watcher);
  dep.notify();
</script>
</body>
</html>